﻿using HandyControl.Controls;
using Super.Hmi.Commands;
using Super.Hmi.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TextBox = HandyControl.Controls.TextBox;

namespace Super.Hmi.Assets.CustomControl
{
    /// <summary>
    /// DatePickRangeControl.xaml 的交互逻辑
    /// </summary>
    public partial class DatePickRangeControl : UserControl
    {
        public DateTime? BeginTime
        {
            get { return (DateTime?)GetValue(BeginTimeProperty); }
            set { SetValue(BeginTimeProperty, value); }
        }
        public static readonly DependencyProperty BeginTimeProperty = DependencyProperty.Register("BeginTime", typeof(DateTime?), typeof(DatePickRangeControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnBeginTimeChanged)));

        private static void OnBeginTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DatePickRangeControl control = d as DatePickRangeControl;
            if (e.NewValue != null)
            {
                DateTime? endTime = (DateTime?)control.GetValue(EndTimeProperty);
                if (endTime != null && (DateTime?)e.NewValue > endTime)
                {
                    control.SetValue(BeginTimeProperty, endTime);
                }
            }
            else
            {
                control.DateTimePickBeginTime.Text = "";
            }
        }

        public DateTime? EndTime
        {
            get { return (DateTime?)GetValue(EndTimeProperty); }
            set { SetValue(EndTimeProperty, value); }
        }
        public static readonly DependencyProperty EndTimeProperty = DependencyProperty.Register("EndTime", typeof(DateTime?), typeof(DatePickRangeControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnEndTimeChanged)));

        private static void OnEndTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DatePickRangeControl control = d as DatePickRangeControl;
            if (e.NewValue != null)
            {
                DateTime? beginTime = (DateTime?)control.GetValue(BeginTimeProperty);
                if (beginTime != null && (DateTime?)e.NewValue < beginTime)
                {
                    control.SetValue(EndTimeProperty, beginTime);
                }
            }
            else
            {
                control.DateTimePickEndTime.Text = "";
            }
        }

        public DatePickRangeControl()
        {
            InitializeComponent();
        }
    }
}
